package com.rop.system.controller;


import com.rop.model.system.SysMenu;
import com.rop.model.vo.AssignMenuVo;
import com.rop.result.Result;
import com.rop.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author rop
 * @since 2023-04-24
 */

@Api(tags = "菜单管理接口")
@Slf4j
@RestController
@RequestMapping("admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 菜单列表查询
     *
     * @return 菜单列表
     */
    @ApiOperation("菜单列表查询")
    @GetMapping("findNodes")
    public Result<List<SysMenu>> findNodes() {
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        return Result.ok(sysMenuList);
    }

    /**
     * 添加菜单
     *
     * @param sysMenu 菜单实体
     * @return 添加结果
     */
    @ApiOperation("添加菜单")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody SysMenu sysMenu) {
        boolean isSuccess = sysMenuService.save(sysMenu);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 修改菜单
     * @param sysMenu 菜单实体
     * @return 修改结果
     */
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody SysMenu sysMenu) {
        boolean isSuccess = sysMenuService.updateById(sysMenu);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 根据id删除菜单
     * @param id 菜单id
     * @return 删除结果
     */
    @ApiOperation("根据id删除菜单")
    @DeleteMapping("remove/{id}")
    public Result<Object> remove(@PathVariable Long id) {
        boolean isSuccess = false;
        try {
            isSuccess = sysMenuService.removeMenuById(id);
            return isSuccess ? Result.ok() : Result.fail();
        } catch (Exception e) {
            log.error("操作失败", e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 根据id查询菜单
     * @param id 菜单id
     * @return 菜单详情
     */
    @ApiOperation("根据id查询菜单")
    @GetMapping("getInfoById/{id}")
    public Result<SysMenu> getInfoById(@PathVariable Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }

    /**
     * 根据角色id获取菜单
     * @param roleId 角色id
     * @return 菜单集合
     */
    @ApiOperation("根据角色id获取菜单")
    @GetMapping("findMenuByRoleId/{roleId}")
    public Result<List<SysMenu>> findMenuByRoleId(@PathVariable Long roleId) {
        List<SysMenu> sysMenuList = sysMenuService.findMenuByRoleId(roleId);
        return Result.ok(sysMenuList);
    }

    /**
     * 给角色分配菜单
     * @param assignMenuVo 菜单vo
     * @return 分配结果
     */
    @ApiOperation("给角色分配菜单")
    @PostMapping("doAssign")
    public Result<Boolean> doAssign(@RequestBody AssignMenuVo assignMenuVo) {
        boolean isSuccess = sysMenuService.doAssign(assignMenuVo);
        return isSuccess ? Result.ok() : Result.fail();
    }
}

